home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-11-08 | 24.6 KB | 716 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: FWStrmF.cpp
- // Release Version: $ 1.0d11 $
- //
- // Copyright: (c) 1993, 1995 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #include "FWFound.hpp"
-
- #ifndef FWSTRMF_H
- #include "FWStrmF.h"
- #endif
-
- #ifndef FWPRISTR_H
- #include "FWPriStr.h"
- #endif
-
- #ifndef FWEXCDEF_H
- #include "FWExcDef.h"
- #endif
-
- #if FW_LIB_EXPORT_PRAGMAS
- #pragma lib_export on
- #endif
-
- #pragma segment FWStream
-
- //========================================================================================
- // CLASS FW_CReadableStreamFormatter
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // FW_CReadableStreamFormatter::FW_CReadableStreamFormatter
- //----------------------------------------------------------------------------------------
-
- FW_CReadableStreamFormatter::FW_CReadableStreamFormatter()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CReadableStreamFormatter::~FW_CReadableStreamFormatter
- //----------------------------------------------------------------------------------------
-
- FW_CReadableStreamFormatter::~FW_CReadableStreamFormatter()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CReadableStreamFormatter::ReadBytes
- //----------------------------------------------------------------------------------------
-
- void FW_CReadableStreamFormatter::ReadBytes(FW_CSink& sink,
- void* destination,
- long count)
- {
- Identity(sink, destination, count, 1);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CReadableStreamFormatter::ReadChars
- //----------------------------------------------------------------------------------------
-
- void FW_CReadableStreamFormatter::ReadChars(FW_CSink& sink,
- char* destination,
- long count)
- {
- Identity(sink, destination, count, sizeof(char));
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CReadableStreamFormatter::ReadSignedChars
- //----------------------------------------------------------------------------------------
-
- void FW_CReadableStreamFormatter::ReadSignedChars(FW_CSink& sink,
- signed char* destination,
- long count)
- {
- Identity(sink, destination, count, sizeof(signed char));
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CReadableStreamFormatter::ReadUnsignedChars
- //----------------------------------------------------------------------------------------
-
- void FW_CReadableStreamFormatter::ReadUnsignedChars(FW_CSink& sink,
- unsigned char* destination,
- long count)
- {
- Identity(sink, destination, count, sizeof(unsigned char));
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CReadableStreamFormatter::ReadNullTerminatedString
- //----------------------------------------------------------------------------------------
-
- void FW_CReadableStreamFormatter::ReadNullTerminatedString(FW_CSink& sink,
- char* destination)
- {
- long totalBytesCopied = 0;
- long availableReadBytes;
- const char * source = (const char *)sink.ReadPeek(availableReadBytes);
- FW_Boolean foundTerminator = FALSE;
-
- if (source!=NULL && availableReadBytes>0)
- {
- // Read sink until null terminator found or no bytes to read.
- while (!foundTerminator && availableReadBytes)
- {
- long i;
- // Copy all characters until null-terminator found or all available bytes copied.
- for (i = 0; !foundTerminator && (i < availableReadBytes); ++i)
- {
- char copyChar = source[i];
- *destination++ = copyChar; // Note: null-terminator is copied too.
- if (copyChar == '\0')
- foundTerminator = TRUE;
- }
-
- // Advance sink position by number of characters processed.
- sink.ReadPeekAdvance(i);
-
- // If null-terminator not found, advance position in sink.
- if (!foundTerminator)
- {
- totalBytesCopied += availableReadBytes;
- source = (const char *)sink.ReadPeek(availableReadBytes);
- }
- }
- }
- else
- {
- availableReadBytes = sink.GetReadableBytes();
- while (!foundTerminator && availableReadBytes)
- {
- sink.Read(destination,1);
- --availableReadBytes;
- ++totalBytesCopied;
- foundTerminator = (*destination++ != '\0');
- }
-
- }
- if (!foundTerminator)
- FW_Failure(FW_xReadableStreamFormatter);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CReadableStreamFormatter::ReadInts
- //----------------------------------------------------------------------------------------
-
- void FW_CReadableStreamFormatter::ReadInts(FW_CSink& sink,
- int* destination,
- long count)
- {
- Identity(sink, destination, count, sizeof(int));
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CReadableStreamFormatter::ReadUnsignedInts
- //----------------------------------------------------------------------------------------
-
- void FW_CReadableStreamFormatter::ReadUnsignedInts(FW_CSink& sink,
- unsigned int* destination,
- long count)
- {
- Identity(sink, destination, count, sizeof(unsigned int));
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CReadableStreamFormatter::ReadShorts
- //----------------------------------------------------------------------------------------
-
- void FW_CReadableStreamFormatter::ReadShorts(FW_CSink& sink,
- short* destination,
- long count)
- {
- Identity(sink, destination, count, sizeof(short));
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CReadableStreamFormatter::ReadUnsignedShorts
- //----------------------------------------------------------------------------------------
-
- void FW_CReadableStreamFormatter::ReadUnsignedShorts(FW_CSink& sink,
- unsigned short* destination,
- long count)
- {
- Identity(sink, destination, count, sizeof(unsigned short));
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CReadableStreamFormatter::ReadLongs
- //----------------------------------------------------------------------------------------
-
- void FW_CReadableStreamFormatter::ReadLongs(FW_CSink& sink,
- long* destination,
- long count)
- {
- Identity(sink, destination, count, sizeof(long));
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CReadableStreamFormatter::ReadUnsignedLongs
- //----------------------------------------------------------------------------------------
-
- void FW_CReadableStreamFormatter::ReadUnsignedLongs(FW_CSink& sink,
- unsigned long* destination,
- long count)
- {
- Identity(sink, destination, count, sizeof(unsigned long));
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CReadableStreamFormatter::ReadFloats
- //----------------------------------------------------------------------------------------
-
- void FW_CReadableStreamFormatter::ReadFloats(FW_CSink& sink,
- float* destination,
- long count)
- {
- Identity(sink, destination, count, sizeof(float));
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CReadableStreamFormatter::ReadDoubles
- //----------------------------------------------------------------------------------------
-
- void FW_CReadableStreamFormatter::ReadDoubles(FW_CSink& sink,
- double* destination,
- long count)
- {
- Identity(sink, destination, count, sizeof(double));
- }
-
- #ifdef FW_COMPILER_SUPPORTS_LONG_DOUBLE
- //----------------------------------------------------------------------------------------
- // FW_CReadableStreamFormatter::ReadLongDoubles
- //----------------------------------------------------------------------------------------
-
- void FW_CReadableStreamFormatter::ReadLongDoubles(FW_CSink& sink,
- long double* destination,
- long count)
- {
- Identity(sink, destination, count, sizeof(long double));
- }
- #endif
-
- //----------------------------------------------------------------------------------------
- // FW_CReadableStreamFormatter::Identity
- //----------------------------------------------------------------------------------------
-
- void FW_CReadableStreamFormatter::Identity(FW_CSink& sink,
- void* destination,
- long count,
- long itemSize)
- {
- char* currentDestination = (char*) destination;
- long totalBytesToRead = count * itemSize;
- long bytesLeftToRead = totalBytesToRead;
- long bytesAvailable = sink.GetReadableBytes();
- long bytesToReadThisTime = FW_Minimum(bytesLeftToRead, bytesAvailable);
-
- while (bytesToReadThisTime)
- {
- sink.Read(currentDestination, bytesToReadThisTime);
- currentDestination += bytesToReadThisTime;
- bytesLeftToRead -= bytesToReadThisTime;
- bytesAvailable = sink.GetReadableBytes();
- bytesToReadThisTime = FW_Minimum(bytesLeftToRead, bytesAvailable);
- }
-
- if (bytesLeftToRead)
- FW_Failure(FW_xReadableStreamFormatter);
- }
-
-
- //========================================================================================
- // CLASS FW_CWritableStreamFormatter
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // FW_CWritableStreamFormatter::FW_CWritableStreamFormatter
- //----------------------------------------------------------------------------------------
-
- FW_CWritableStreamFormatter::FW_CWritableStreamFormatter()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CWritableStreamFormatter::~FW_CWritableStreamFormatter
- //----------------------------------------------------------------------------------------
-
- FW_CWritableStreamFormatter::~FW_CWritableStreamFormatter()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CWritableStreamFormatter::WriteBytes
- //----------------------------------------------------------------------------------------
-
- void FW_CWritableStreamFormatter::WriteBytes(FW_CSink& sink,
- const void* source,
- long count)
- {
- Identity(sink, source, count, 1);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CWritableStreamFormatter::WriteChars
- //----------------------------------------------------------------------------------------
-
- void FW_CWritableStreamFormatter::WriteChars(FW_CSink& sink,
- const char* source,
- long count)
- {
- Identity(sink, source, count, sizeof(char));
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CWritableStreamFormatter::WriteSignedChars
- //----------------------------------------------------------------------------------------
-
- void FW_CWritableStreamFormatter::WriteSignedChars(FW_CSink& sink,
- const signed char* source,
- long count)
- {
- Identity(sink, source, count, sizeof(signed char));
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CWritableStreamFormatter::WriteUnsignedChars
- //----------------------------------------------------------------------------------------
-
- void FW_CWritableStreamFormatter::WriteUnsignedChars(FW_CSink& sink,
- const unsigned char* source,
- long count)
- {
- Identity(sink, source, count, sizeof(unsigned char));
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CWritableStreamFormatter::WriteNullTerminatedString
- //----------------------------------------------------------------------------------------
-
- void FW_CWritableStreamFormatter::WriteNullTerminatedString(FW_CSink& sink,
- const char* source)
- {
- Identity(sink, source, FW_PrimitiveStringLength(source) + 1, 1);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CWritableStreamFormatter::WriteInts
- //----------------------------------------------------------------------------------------
-
- void FW_CWritableStreamFormatter::WriteInts(FW_CSink& sink,
- const int* source,
- long count)
- {
- Identity(sink, source, count, sizeof(int));
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CWritableStreamFormatter::WriteUnsignedInts
- //----------------------------------------------------------------------------------------
-
- void FW_CWritableStreamFormatter::WriteUnsignedInts(FW_CSink& sink,
- const unsigned int* source,
- long count)
- {
- Identity(sink, source, count, sizeof(unsigned int));
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CWritableStreamFormatter::WriteShorts
- //----------------------------------------------------------------------------------------
-
- void FW_CWritableStreamFormatter::WriteShorts(FW_CSink& sink,
- const short* source,
- long count)
- {
- Identity(sink, source, count, sizeof(short));
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CWritableStreamFormatter::WriteUnsignedShorts
- //----------------------------------------------------------------------------------------
-
- void FW_CWritableStreamFormatter::WriteUnsignedShorts(FW_CSink& sink,
- const unsigned short* source,
- long count)
- {
- Identity(sink, source, count, sizeof(unsigned short));
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CWritableStreamFormatter::WriteLongs
- //----------------------------------------------------------------------------------------
-
- void FW_CWritableStreamFormatter::WriteLongs(FW_CSink& sink,
- const long* source,
- long count)
- {
- Identity(sink, source, count, sizeof(long));
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CWritableStreamFormatter::WriteUnsignedLongs
- //----------------------------------------------------------------------------------------
-
- void FW_CWritableStreamFormatter::WriteUnsignedLongs(FW_CSink& sink,
- const unsigned long* source,
- long count)
- {
- Identity(sink, source, count, sizeof(unsigned long));
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CWritableStreamFormatter::WriteFloats
- //----------------------------------------------------------------------------------------
-
- void FW_CWritableStreamFormatter::WriteFloats(FW_CSink& sink,
- const float* source,
- long count)
- {
- Identity(sink, source, count, sizeof(float));
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CWritableStreamFormatter::WriteDoubles
- //----------------------------------------------------------------------------------------
-
- void FW_CWritableStreamFormatter::WriteDoubles(FW_CSink& sink,
- const double* source,
- long count)
- {
- Identity(sink, source, count, sizeof(double));
- }
-
- #ifdef FW_COMPILER_SUPPORTS_LONG_DOUBLE
- //----------------------------------------------------------------------------------------
- // FW_CWritableStreamFormatter::WriteLongDoubles
- //----------------------------------------------------------------------------------------
-
- void FW_CWritableStreamFormatter::WriteLongDoubles(FW_CSink& sink,
- const long double* source,
- long count)
- {
- Identity(sink, source, count, sizeof(long double));
- }
- #endif
-
- //----------------------------------------------------------------------------------------
- // FW_CWritableStreamFormatter::Identity
- //----------------------------------------------------------------------------------------
-
- void FW_CWritableStreamFormatter::Identity(FW_CSink& sink,
- const void* source,
- long count,
- long itemSize)
- {
- const char* currentSource = (const char*) source;
- long totalBytesToWrite = count * itemSize;
- long bytesLeftToWrite = totalBytesToWrite;
- long bytesAvailable = sink.GetWritableBytes();
- long bytesToWriteThisTime = FW_Minimum(bytesLeftToWrite, bytesAvailable);
-
- while (bytesToWriteThisTime)
- {
- sink.Write(currentSource, bytesToWriteThisTime);
- currentSource += bytesToWriteThisTime;
- bytesLeftToWrite -= bytesToWriteThisTime;
- bytesAvailable = sink.GetWritableBytes();
- bytesToWriteThisTime = FW_Minimum(bytesLeftToWrite, bytesAvailable);
- }
-
- if (bytesLeftToWrite)
- FW_Failure(FW_xWritableStreamFormatter);
- }
-
-
- //======================================================================================
- // class FW_CByteSwapper
- //======================================================================================
-
- //----------------------------------------------------------------------------------------
- // FW_CByteSwapper::SwapShort
- //----------------------------------------------------------------------------------------
-
- void FW_CByteSwapper::SwapShort(void *aShort)
- {
- char* s = (char*)aShort;
- char c = s[0];
- s[0] = s[1];
- s[1] = c;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CByteSwapper::SwapLong
- //----------------------------------------------------------------------------------------
-
- void FW_CByteSwapper::SwapLong(void *aLong)
- {
- char* s = (char*)aLong;
- char c = s[0];
- s[0] = s[3];
- s[3] = c;
- c = s[1];
- s[1] = s[2];
- s[2] = c;
- }
-
- //======================================================================================
- // class FW_CReadableSwapBytesFormatter
- //======================================================================================
-
- //----------------------------------------------------------------------------------------
- // FW_CReadableSwapBytesFormatter::FW_CReadableSwapBytesFormatter
- //----------------------------------------------------------------------------------------
-
- FW_CReadableSwapBytesFormatter::FW_CReadableSwapBytesFormatter()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CReadableSwapBytesFormatter::~FW_CReadableSwapBytesFormatter
- //----------------------------------------------------------------------------------------
-
- FW_CReadableSwapBytesFormatter::~FW_CReadableSwapBytesFormatter()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CReadableSwapBytesFormatter::ReadInts
- //----------------------------------------------------------------------------------------
-
- void FW_CReadableSwapBytesFormatter::ReadInts(FW_CSink& sink,
- int* destination,
- long count)
- {
- #if FW_FOUR_BYTE_INTS
- ReadLongs(sink, (long*)destination, count);
- #else
- ReadShorts(sink, (short*)destination, count);
- #endif
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CReadableSwapBytesFormatter::ReadUnsignedInts
- //----------------------------------------------------------------------------------------
-
- void FW_CReadableSwapBytesFormatter::ReadUnsignedInts(FW_CSink& sink,
- unsigned int* destination,
- long count)
- {
- ReadInts(sink, (int*)destination, count);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CReadableSwapBytesFormatter::ReadShorts
- //----------------------------------------------------------------------------------------
-
- void FW_CReadableSwapBytesFormatter::ReadShorts(FW_CSink& sink,
- short* destination,
- long count)
- {
- FW_CReadableStreamFormatter::ReadShorts(sink, destination, count);
-
- short* p = destination;
- while (p < destination + count)
- {
- FW_CByteSwapper::SwapShort(p);
- p++;
- }
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CReadableSwapBytesFormatter::ReadUnsignedShorts
- //----------------------------------------------------------------------------------------
-
- void FW_CReadableSwapBytesFormatter::ReadUnsignedShorts(FW_CSink& sink,
- unsigned short* destination,
- long count)
- {
- ReadShorts(sink, (short*)destination, count);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CReadableSwapBytesFormatter::ReadLongs
- //----------------------------------------------------------------------------------------
-
- void FW_CReadableSwapBytesFormatter::ReadLongs(FW_CSink& sink,
- long* destination,
- long count)
- {
- FW_CReadableStreamFormatter::ReadLongs(sink, destination, count);
-
- long* p = destination;
- while (p < destination + count)
- {
- FW_CByteSwapper::SwapLong(p);
- p++;
- }
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CReadableSwapBytesFormatter::ReadUnsignedLongs
- //----------------------------------------------------------------------------------------
-
- void FW_CReadableSwapBytesFormatter::ReadUnsignedLongs(FW_CSink& sink,
- unsigned long* destination,
- long count)
- {
- ReadLongs(sink, (long*)destination, count);
- }
-
- //======================================================================================
- // class FW_CWritableSwapBytesFormatter
- //======================================================================================
-
- //----------------------------------------------------------------------------------------
- // FW_CWritableSwapBytesFormatter::ReadLongs
- //----------------------------------------------------------------------------------------
-
- FW_CWritableSwapBytesFormatter::FW_CWritableSwapBytesFormatter()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CWritableSwapBytesFormatter::ReadLongs
- //----------------------------------------------------------------------------------------
-
- FW_CWritableSwapBytesFormatter::~FW_CWritableSwapBytesFormatter()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CWritableSwapBytesFormatter::WriteInts
- //----------------------------------------------------------------------------------------
-
- void FW_CWritableSwapBytesFormatter::WriteInts(FW_CSink& sink,
- const int* source,
- long count)
- {
- #if FW_FOUR_BYTE_INTS
- WriteLongs(sink, (const long*)source, count);
- #else
- WriteShorts(sink, (const short*)source, count);
- #endif
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CWritableSwapBytesFormatter::WriteUnsignedInts
- //----------------------------------------------------------------------------------------
-
- void FW_CWritableSwapBytesFormatter::WriteUnsignedInts(FW_CSink& sink,
- const unsigned int* source,
- long count)
- {
- WriteInts(sink, (const int *)source, count);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CWritableSwapBytesFormatter::WriteShorts
- //----------------------------------------------------------------------------------------
-
- void FW_CWritableSwapBytesFormatter::WriteShorts(FW_CSink& sink,
- const short* source,
- long count)
- {
- while (count-- != 0)
- {
- short aShort = *source++;
- FW_CByteSwapper::SwapShort(&aShort);
- Identity(sink, &aShort, 1, sizeof(short));
- }
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CWritableSwapBytesFormatter::WriteUnsignedShorts
- //----------------------------------------------------------------------------------------
-
- void FW_CWritableSwapBytesFormatter::WriteUnsignedShorts(FW_CSink& sink,
- const unsigned short* source,
- long count)
- {
- WriteShorts(sink, (const short *)source, count);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CWritableSwapBytesFormatter::WriteLongs
- //----------------------------------------------------------------------------------------
-
- void FW_CWritableSwapBytesFormatter::WriteLongs(FW_CSink& sink,
- const long* source,
- long count)
- {
- while (count-- != 0)
- {
- long aLong = *source++;
- FW_CByteSwapper::SwapLong(&aLong);
- Identity(sink, &aLong, 1, sizeof(long));
- }
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CWritableSwapBytesFormatter::WriteUnsignedLongs
- //----------------------------------------------------------------------------------------
-
- void FW_CWritableSwapBytesFormatter::WriteUnsignedLongs(FW_CSink& sink,
- const unsigned long* source,
- long count)
- {
- WriteLongs(sink, (const long *)source, count);
- }
-